<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Validation\ValidationException;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Support\Arr;
use EasyWeChat\Kernel\Messages\Text;
use EasyWeChat\Kernel\Exceptions\Exception as EasyBaseException;
use Log;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that should not be reported.
     *
     * @var array
     */
    protected $dontReport = [
        AuthorizationException::class,
        HttpException::class,
        ModelNotFoundException::class,
        ValidationException::class,
    ];

    /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @param  \Exception  $e
     * @return void
     */
    public function report(Exception $e)
    {
        parent::report($e);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $e
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $e)
    {
		/*程序有报错时通知开发者*/			
		$code=$this->isHttpException($e) ? $e->getStatusCode() : 500;
		
		if (!($e instanceof EasyBaseException)&&!env('APP_DEBUG')&&$code==500) {		
			$app = app('wechat.official_account');
			$message = new Text('你的程序报真的报错了快去查查');			
			$result = $app->customer_service->message($message)->to('oxVwMwBJzpxRX5tr3xD831Wm0icQ')->send();		
		}	
		
        return parent::render($request, $e);
    }
	
    protected function invalidJson($request, ValidationException $exception)
    {
        return response()->json([
			'code'=>$exception->status,
            'msg' => '数据验证不正确',
            'error' => $exception->errors(),
			'data'=>''
        ], 200);
    }
	
    /**
     * Convert the given exception to an array.
     *
     * @param  \Exception  $e
     * @return array
     */
    protected function convertExceptionToArray(Exception $e)
    {
        return config('app.debug') ? [
            'message' => $e->getMessage(),
            'exception' => get_class($e),
            'file' => $e->getFile(),
            'line' => $e->getLine(),
            'trace' => collect($e->getTrace())->map(function ($trace) {
                return Arr::except($trace, ['args']);
            })->all(),
        ] : [
            'code'=>$this->isHttpException($e) ? $e->getStatusCode() : 500,'msg' => $this->isHttpException($e) ? $e->getMessage() : 'Server Error','data'=>[],
        ];
    }
	
    /**
     * Convert an authentication exception into a response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Illuminate\Auth\AuthenticationException  $exception
     * @return \Illuminate\Http\Response
     */
    protected function unauthenticated($request, AuthenticationException $exception)
    {
        return $request->expectsJson()
                    ? response()->json(['code'=>401,'data'=>[],'msg' => $exception->getMessage()], 401)
                    : redirect()->guest(route('login'));
    }
}
